package com.ucar.train.web;

import com.google.common.base.Strings;
import com.ucar.train.dto.*;
import com.ucar.train.service.CrmService;
import com.ucar.train.service.OrderDetailService;
import com.ucar.train.service.OrderService;
import com.ucar.train.service.impl.CarServiceImp;
import com.ucar.train.service.impl.StoreServiceImpl;
import com.ucar.train.utils.JodaUtils;
import com.ucar.train.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wufuqiang
 * @title: CrmController
 * @projectName:train-parent
 * @description: 用户
 * @created 2019-08-14 10:55
 */
@Controller
@RequestMapping("/crm")
public class CrmController {


    @Autowired
    CrmService crmService;

    @Autowired
    OrderService orderService;

    @Autowired
    OrderDetailService orderDetailService;

    @Autowired
    private CarServiceImp carService;

    @Autowired
    private StoreServiceImpl storeService;
    /**
     * @Author: wufuqiang
     * @Description:返回登录页面
     * @Param:
     * @return:
     * @Date: 2019/8/14
     */
    @RequestMapping(value = "/login",method = RequestMethod.GET)
    public String login(){
        return "crmLogin";
    }

    /**
     * @Author: wufuqiang
     * @escription: 登入
     * @Para: crmVO
     * @return:
     * @Date: 2019/8/14
     */
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String login(CrmVO crmVO, HttpServletRequest req){
        CrmDTO crmDTO = new CrmDTO();
        crmDTO.setUsername(crmVO.getUsername());
        crmDTO.setPassword(crmVO.getPassword());

        CrmDTO crmDTOTemp = crmService.login(crmDTO);
        if(crmDTOTemp != null){
            HttpSession session = req.getSession();
            session.setAttribute("crm",crmDTOTemp);
            return "index";
        }
        return "crmLogin";
    }

    /**
     * @Author: wufuqiang
     * @Description:返回个人用户注册页面
     * @Param:
     * @return:
     * @Date: 2019/8/14
     */
    @RequestMapping(value = "/personRegister",method = RequestMethod.GET)
    public String personRegister(){
        return "personRegister";
    }

    /**
     * @Author: wufuqiang
     * @Description: 个人用户注册
     * @Param:a
     * @return:a
     * @Date: 2019/8/14
     */
    @RequestMapping(value = "/personRegister",method = RequestMethod.POST)
    public String personRegister(CrmPersonVO crmPersonVO){
        //用户基本信息
        CrmDTO crmDTO = new CrmDTO();
        crmDTO.setUsername(crmPersonVO.getUsername());
        crmDTO.setPassword(crmPersonVO.getPassword());
        crmDTO.setPhone(crmPersonVO.getPhone());
        crmDTO.setEmail(crmPersonVO.getEmail());
        crmDTO.setType(1);
        crmDTO.setState(1);
        crmDTO.setCreateTime(JodaUtils.nowAsDate());
        crmDTO.setModifyTime(JodaUtils.nowAsDate());

        //用户特有信息
        CrmPersonDTO crmPersonDTO = new CrmPersonDTO();
        crmPersonDTO.setName("");
        crmPersonDTO.setNumber("");
        crmPersonDTO.setLicense("");
        crmPersonDTO.setCreateTime(JodaUtils.nowAsDate());
        crmPersonDTO.setModifyTime(JodaUtils.nowAsDate());

        Integer i = crmService.personRegister(crmDTO,crmPersonDTO);
        if(i == 1){
            return "crmLogin";
        }else{
            return "personRegister";
        }

    }

    /**
     * @Author: wufuqiang
     * @escription:返回企业用户注册页面
     * @Para: a
     * @return: a
     * @Date: 2019/8/14
     */
    @RequestMapping(value = "/enterpriseRegister",method = RequestMethod.GET)
    public String enterpriseRegister(){
        return "enterpriseRegister";
    }

    /**
     * @Author: wufuqiang
     * @Description: 企业用户注册
     * @Param:a
     * @return:a
     * @Date: 2019/8/14
     */
    @RequestMapping(value = "/enterpriseRegister",method = RequestMethod.POST)
    public String enterpriseRegister(CrmEnterpriseVO crmEnterpriseVO){
        CrmDTO crmDTO = new CrmDTO();
        crmDTO.setUsername(crmEnterpriseVO.getUsername());
        crmDTO.setPassword(crmEnterpriseVO.getPassword());
        crmDTO.setPhone(crmEnterpriseVO.getPhone());
        crmDTO.setEmail(crmEnterpriseVO.getEmail());
        crmDTO.setType(2);
        crmDTO.setState(1);
        crmDTO.setCreateTime(JodaUtils.nowAsDate());
        crmDTO.setModifyTime(JodaUtils.nowAsDate());

        CrmEnterpriseDTO crmEnterpriseDTO = new CrmEnterpriseDTO();
        crmEnterpriseDTO.setName(crmEnterpriseVO.getName());
        crmEnterpriseDTO.setAddress(crmEnterpriseVO.getAddress());
        crmEnterpriseDTO.setContact(crmEnterpriseVO.getContact());
        crmEnterpriseDTO.setContactNumber(crmEnterpriseVO.getContactNumber());
        crmEnterpriseDTO.setIndustrNumber(crmEnterpriseVO.getIndustrNumber());
        crmEnterpriseDTO.setCreateTime(JodaUtils.nowAsDate());
        crmEnterpriseDTO.setModifyTime(JodaUtils.nowAsDate());
        Integer i = crmService.enterpriseRegister(crmDTO,crmEnterpriseDTO);
        if(i == 1){
            return "crmLogin";
        }else{
            return "enterpriseRegister";
        }
    }

    /**
     * @Author: wufuqiang
     * @Description:查看我的信息
     * @Param:a
     * @return:a
     * @Date: 2019/8/14
     */
    @RequestMapping(value = "/getMyInfo",method = RequestMethod.GET)
    public String getMyInfo(HttpServletRequest req, Model model){
        HttpSession session = req.getSession();
        CrmDTO crmDTO = (CrmDTO) session.getAttribute("crm");
        List<OrderDTO> orderDTOS=orderService.SelectOrdersByCustomerId(crmDTO.getId());
        List<OrderVO> orderVOS=new ArrayList<>();

        if(crmDTO.getType() == 1){
            CrmPersonVO crmPersonVO = (CrmPersonVO) crmService.getCrm(crmDTO);

            //
            for (OrderDTO orderDTO : orderDTOS) {
                OrderDetailDTO orderDetailDTO = orderDetailService.selectByOrderNo(orderDTO.getOrderNo());
                CarDTO carDTO = carService.selectCarDTOByCarId(orderDetailDTO.getCarId());
                StoreDTO getStoreStoreDTO = storeService.selectStorebyStoreId(orderDetailDTO.getGetStoreCode());//通过storeCode查出门店信息
                StoreDTO backStoreStoreDTO = storeService.selectStorebyStoreId(orderDetailDTO.getBackStoreCode());
                OrderVO orderVO = new OrderVO();
                orderVO.setOrderNo(orderDTO.getOrderNo());
                orderVO.setCarNumber(orderDTO.getCarNumber());
                orderVO.setType(carDTO.getType());//调Car接口
                if (!Strings.isNullOrEmpty(orderDetailDTO.getGetStoreCode())) {
                    orderVO.setGetStoreCode(getStoreStoreDTO.getName());
                } else {
                    orderVO.setGetStoreCode("暂未取车");
                }
                if (!Strings.isNullOrEmpty(orderDetailDTO.getBackStoreCode())) {
                    orderVO.setBackStoreCode(backStoreStoreDTO.getName());
                } else {
                    orderVO.setBackStoreCode("暂未还车");
                }
                orderVO.setGetTime(JodaUtils.dateToString(orderDetailDTO.getGetTime(), "yyyy-MM-dd HH:mm:ss"));
                orderVO.setBackTime(JodaUtils.dateToString(orderDetailDTO.getBackTime(), "yyyy-MM-dd HH:mm:ss"));
                orderVO.setRent(orderDTO.getRent());
                orderVO.setCost(orderDTO.getCost());
                orderVO.setState(orderDTO.getState());
                orderVOS.add(orderVO);
            }

            model.addAttribute("crmPerson",crmPersonVO);
            model.addAttribute("orders",orderVOS);
            return "crmPerson-show";
        }


        CrmEnterpriseVO crmEnterpriseVO = (CrmEnterpriseVO) crmService.getCrm(crmDTO);

        //
        for (OrderDTO orderDTO : orderDTOS) {
            OrderDetailDTO orderDetailDTO = orderDetailService.selectByOrderNo(orderDTO.getOrderNo());
            CarDTO carDTO = carService.selectCarDTOByCarId(orderDetailDTO.getCarId());
            StoreDTO getStoreStoreDTO = storeService.selectStorebyStoreId(orderDetailDTO.getGetStoreCode());//通过storeCode查出门店信息
            StoreDTO backStoreStoreDTO = storeService.selectStorebyStoreId(orderDetailDTO.getBackStoreCode());
            OrderVO orderVO = new OrderVO();
            orderVO.setOrderNo(orderDTO.getOrderNo());
            orderVO.setCarNumber(orderDTO.getCarNumber());
            orderVO.setType(carDTO.getType());//调Car接口
            if (!Strings.isNullOrEmpty(orderDetailDTO.getGetStoreCode())) {
                orderVO.setGetStoreCode(getStoreStoreDTO.getName());
            } else {
                orderVO.setGetStoreCode("暂未取车");
            }
            if (!Strings.isNullOrEmpty(orderDetailDTO.getBackStoreCode())) {
                orderVO.setBackStoreCode(backStoreStoreDTO.getName());
            } else {
                orderVO.setBackStoreCode("暂未还车");
            }
            orderVO.setGetTime(JodaUtils.dateToString(orderDetailDTO.getGetTime(), "yyyy-MM-dd HH:mm:ss"));
            orderVO.setBackTime(JodaUtils.dateToString(orderDetailDTO.getBackTime(), "yyyy-MM-dd HH:mm:ss"));
            orderVO.setRent(orderDTO.getRent());
            orderVO.setCost(orderDTO.getCost());
            orderVO.setState(orderDTO.getState());
            orderVOS.add(orderVO);
        }

        model.addAttribute("crmEnterprise",crmEnterpriseVO);
        model.addAttribute("orders",orderVOS);
        return "crmEnterprise-show";
    }

    /**
     * @Author: wufuqiang
     * @Description: 修改企业用户信息
     * @Param:a
     * @return:a
     * @Date: 2019/8/14
     */
    @RequestMapping(value = "/updateEnterpriseInfo",method = RequestMethod.POST)
    public String updateMyInfo(CrmEnterpriseVO crmEnterpriseVO){
        CrmDTO crmDTO = new CrmDTO();

        crmDTO.setId(crmEnterpriseVO.getId());
        crmDTO.setPhone(crmEnterpriseVO.getPhone());
        crmDTO.setEmail(crmEnterpriseVO.getEmail());
        crmDTO.setModifyTime(JodaUtils.nowAsDate());

        CrmEnterpriseDTO crmEnterpriseDTO = new CrmEnterpriseDTO();
        crmEnterpriseDTO.setId(crmEnterpriseVO.getId());
        crmEnterpriseDTO.setName(crmEnterpriseVO.getName());
        crmEnterpriseDTO.setAddress(crmEnterpriseVO.getAddress());
        crmEnterpriseDTO.setContact(crmEnterpriseVO.getContact());
        crmEnterpriseDTO.setContactNumber(crmEnterpriseVO.getContactNumber());
        crmEnterpriseDTO.setIndustrNumber(crmEnterpriseVO.getIndustrNumber());
        crmEnterpriseDTO.setModifyTime(JodaUtils.nowAsDate());

        crmService.updateEnterpriseCrm(crmDTO,crmEnterpriseDTO);
        return "redirect:getMyInfo";
    }

    /**
     * @Author: wufuqiang
     * @Description:修改个人用户信息
     * @Param:a
     * @return:a
     * @Date: 2019/8/14
     */
    @RequestMapping(value = "/updatePersonInfo",method = RequestMethod.POST)
    public String updateMyInfo(CrmPersonVO crmPersonVO){

        System.out.println("修改crmPeron" + crmPersonVO);


        //用户基本信息
        CrmDTO crmDTO = new CrmDTO();
        crmDTO.setId(crmPersonVO.getId());
        crmDTO.setPhone(crmPersonVO.getPhone());
        crmDTO.setEmail(crmPersonVO.getEmail());
        crmDTO.setModifyTime(JodaUtils.nowAsDate());

        //用户特有信息
        CrmPersonDTO crmPersonDTO = new CrmPersonDTO();
        crmPersonDTO.setId(crmPersonVO.getId());
        crmPersonDTO.setName(crmPersonVO.getName());
        crmPersonDTO.setNumber(crmPersonVO.getNumber());
        crmPersonDTO.setLicense(crmPersonVO.getLicense());
        crmPersonDTO.setModifyTime(JodaUtils.nowAsDate());

        crmService.updatePersonCrm(crmDTO,crmPersonDTO);

        return "redirect:getMyInfo";
    }

    @RequestMapping(value = "/orderCar",method = RequestMethod.GET)
    public ModelAndView orderCar(HttpServletRequest httpServletRequest) {

        HttpSession httpSession = httpServletRequest.getSession();
        CrmDTO crm = (CrmDTO)httpSession.getAttribute("crm");
        System.out.println(crm);
        List<StoreDTO> storeDTOS = crmService.selectStore();
        //DTO转VO
        List<StoreVO> storeVOS = new ArrayList<>();
        for(StoreDTO storeDTO:storeDTOS){
            StoreVO storeVO = new StoreVO();
            storeVO.setStoreCode(storeDTO.getStoreCode());
            storeVO.setId(storeDTO.getId());
            storeVO.setName(storeDTO.getName());
            storeVOS.add(storeVO);
        }
//        System.out.println(storeVOS);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("store",storeVOS);
        CrmDTO crmDTO = crmService.selectCrmById(crm.getId());
//        System.out.println(crmDTO);
        //个人客户
        if(crmDTO.getType().equals(1)){
            CrmPersonDTO crmPersonDTO = crmService.selectCrmPersonById(crm.getId());
            CrmPersonVO crmPersonVO = new CrmPersonVO();
            crmPersonVO.setId(crm.getId());
            crmPersonVO.setPhone(crmDTO.getPhone());
            crmPersonVO.setName(crmPersonDTO.getName());
            crmPersonVO.setUsername(crmDTO.getUsername());
            crmPersonVO.setNumber(crmPersonDTO.getNumber());
            crmPersonVO.setLicense(crmPersonVO.getLicense());
            modelAndView.addObject("customer",crmPersonVO);
            modelAndView.setViewName("customerPersonOrder");
        }else {
            CrmEnterpriseDTO crmEnterpriseDTO = crmService.selectCrmEnterpriseById(crm.getId());
            System.out.println(crmEnterpriseDTO);
            CrmEnterpriseVO crmEnterpriseVO = new CrmEnterpriseVO();
            crmEnterpriseVO.setId(crm.getId());
            crmEnterpriseVO.setUsername(crmDTO.getUsername());
            crmEnterpriseVO.setName(crmEnterpriseDTO.getName());
            crmEnterpriseVO.setContact(crmEnterpriseDTO.getContact());
            crmEnterpriseVO.setContactNumber(crmEnterpriseDTO.getContactNumber());
            crmEnterpriseVO.setAddress(crmEnterpriseDTO.getAddress());
            crmEnterpriseVO.setIndustrNumber(crmEnterpriseDTO.getIndustrNumber());
            modelAndView.addObject("customer",crmEnterpriseVO);
            modelAndView.setViewName("customerEnterpriseOrder");
        }

        return modelAndView;
    }

}
